package com.jinboe.document.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.jinboe.comm.util.BeanUtil;

public class DocumentDefinitionHelper {
	protected static String basePase="D:/workspace/yeabar/";

	public static void main(String[] args) {
		DocumentDefinition documentDefinition= initDocumentDefinition(basePase+"DocumentDefinition.xml");
		System.out.println(DocumentDefinition.getDefinitionType("dataItem"));
	}
	public static DocumentDefinition initDocumentDefinition(String filePath) {  
    	DocumentDefinition documentDefinition=new DocumentDefinition();
        File inputXml=new File(filePath);     
        SAXReader saxReader = new SAXReader();     
        try {     
            Document document = saxReader.read(inputXml);     
            Element rootElement=document.getRootElement();
            for(Iterator i = rootElement.elementIterator(); i.hasNext();){
            	
            	Element element=(Element)i.next(); 
            	System.out.println("1   "+element.getName());
            	try {
					BeanUtil.invokeSetterMethod(documentDefinition, element.getName(), getElementValues(element));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					//System.out.println("---------------初始化DocumentDefinition失败--------------------");
					e.printStackTrace();
				}
            }
        } catch (DocumentException e) {     
            System.out.println(e.getMessage());     
        }
        return documentDefinition;
    } 
    public static List<String> getElementValues(Element element){
    	if(element==null){
    		return null;
    	}
    	List<String> elementTests=new ArrayList();
    	for(Iterator j = element.elementIterator(); j.hasNext();){     
            Element node=(Element) j.next();     
            elementTests.add(node.getTextTrim());  
            System.out.println("2   "+node.getTextTrim());
        } 
    	return elementTests;
    }
}
